package com.najoua.irrverds;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private EditText searchEdt;
	private Button serButton;
	private TextView resultTv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		searchEdt=(EditText)findViewById(R.id.verbs_field);
		serButton =(Button)findViewById(R.id.searsh_btn);
		resultTv = (TextView)findViewById(R.id.textView2);
		
		serButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				
				String verb= searchEdt.getText().toString();
				String answer = getAnswer(verb);
				
				if (answer== null){
					resultTv.setText("NO RESULT");
					
					
				}else {
					
					resultTv.setText(answer);
				}
				
			}
		});
	}

	protected String getAnswer(String verb) {
	
		String answer = null;
		
		if(verb==null || verb.equalsIgnoreCase("")){

			Toast.makeText(getBaseContext(), "Entrez un verbe svp ", Toast.LENGTH_LONG).show();
			
		}else{
			
			try {
				InputStreamReader isr = new InputStreamReader(getAssets().open("verbs.txt"));
				
				BufferedReader br = new BufferedReader(isr);
				
				String ligne = null;
				
				while ( (ligne=br.readLine())!=null) {
					if (verb.equals(ligne.substring(0, ligne.indexOf("")))){
						
						answer =  ligne;
					}
					
					if (answer == null){
						Toast.makeText(getBaseContext(), "Just add ed ",  Toast.LENGTH_LONG).show();
						
						return answer;
					}
					
				}
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		return null;
	}



}
